﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using EstimateTracker.DataAccess.Repositories;
using EstimateTracker.Domain.Entities;
using NUnit.Framework;

namespace EstimateTracker.DataAccess.Tests.Repositories
{
    [TestFixture]
    public class TaskRepositoryTest
    {
        [Test]
        public void FindActualsForDay_WithPopulatedDatabase_FindsActuals()
        {
            // Arrange
            PersistenceManager persistenceManager = new PersistenceManager();
            var taskRepository = new TaskRepository(persistenceManager);
            var projectRepository = new ProjectRepository(persistenceManager);
            var actRepository = new ActivityRepository(persistenceManager);
            var hoursRepository = new HoursWorkedRepository(persistenceManager);
            
            Project p = new Project{Name = "P1"};
            Activity act1 = new Activity {Name = "Act1", Project = p};
            Activity act2 = new Activity {Name = "Act2", Project = p};
            Task t1 = new Task{Activity = act1,EstimatedHours = 3,Name = "t1"};
            Task t2 = new Task{Activity = act1,EstimatedHours = 7,Name = "t2"};
            Task t3 = new Task{Activity = act2,EstimatedHours = 9,Name = "t3"};
            Task t4 = new Task{Activity = act2,EstimatedHours = 6,Name = "t4"};
            HoursWorked hw1 = new HoursWorked {DateAndTimeFinished = DateTime.Now.AddDays(5), Hours = 1, Task = t1};
            HoursWorked hw2 = new HoursWorked {DateAndTimeFinished = DateTime.Now.AddDays(5), Hours = 1.5, Task = t1};
            HoursWorked hw3 = new HoursWorked {DateAndTimeFinished = DateTime.Now.AddDays(5), Hours = 3.5, Task = t2};
            HoursWorked hw4 = new HoursWorked {DateAndTimeFinished = DateTime.Now.AddDays(5), Hours = 2, Task = t2};
            HoursWorked hw5 = new HoursWorked {DateAndTimeFinished = DateTime.Now.AddDays(5), Hours = 4.4, Task = t3};
            HoursWorked hw6 = new HoursWorked {DateAndTimeFinished = DateTime.Now.AddDays(5), Hours = 3.4, Task = t3};
            HoursWorked hw7 = new HoursWorked {DateAndTimeFinished = DateTime.Now.AddDays(5), Hours = 3.4, Task = t4};
            HoursWorked hw8 = new HoursWorked {DateAndTimeFinished = DateTime.Now.AddDays(5), Hours = 1.2, Task = t4};

                
            // Act
            using (var scope = new TransactionScope())
            {
                projectRepository.Save(p);
                actRepository.Save(act1);
                actRepository.Save(act2);
                taskRepository.Save(t1);
                taskRepository.Save(t2);
                taskRepository.Save(t3);
                taskRepository.Save(t4);
                hoursRepository.Save(hw1);
                hoursRepository.Save(hw2);
                hoursRepository.Save(hw3);
                hoursRepository.Save(hw4);
                hoursRepository.Save(hw5);
                hoursRepository.Save(hw6);
                hoursRepository.Save(hw7);
                hoursRepository.Save(hw8);
                IEnumerable<object[]> findActualsForDay = taskRepository.FindActualsForDay(DateTime.Now.AddDays(5));    
                Assert.That(findActualsForDay.Count(),Is.EqualTo(4));
            }
            
            // Assert
            
        } 
    }
}